package com.csii.mobilebanking.tool;

import java.util.List;

import com.csii.mobilebanking.user.TResponse;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.telephony.TelephonyManager;

public class HpUtils
{
	public static boolean isOk(TResponse aResponse){
		if (aResponse != null){
			return aResponse.Succed;
		}
		return false;
	}
	
	public static String getMemo(TResponse aResponse){
		if (aResponse == null){
			return "";
		}
		else{
			if (aResponse.Memo == null){
				return "";
			}
			else{
				return aResponse.Memo;
			}
		}
	}
	
	public static String getErrcode(TResponse aResponse){
		if (aResponse == null){
			return "-1";
		}
		else{
	
		 return aResponse.ErrorCode+"";
			
		}
	}	

	//判断网络是否连接
	public static boolean getNetworkState(Context mContext){
		int i_conn = 0;
		ConnectivityManager connectivityManager = (ConnectivityManager) mContext
				.getApplicationContext().getSystemService("connectivity");
		if (connectivityManager != null) {
			NetworkInfo[] networkInfos = connectivityManager.getAllNetworkInfo();
			if (networkInfos != null) {
				for (int j = 0; j < networkInfos.length; j++) {
					if (networkInfos[j].getState() == NetworkInfo.State.CONNECTED) {
						i_conn++;
					}
				}
			}
		}
		return i_conn > 0;
	}
	//得到网络连接状态
	public static int getNetState(Context context){
		try{
			ConnectivityManager conMan = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
			State Mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
			State Wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
			if (Wifi == State.CONNECTED){
				return HpConstants.NetWifi;
			}
			else {
				if (Mobile == State.CONNECTED){
					return HpConstants.NetMobile;
				}
				else {
					return HpConstants.NetNo; 
				}
			}
		} catch (Exception e)
		{
			return HpConstants.NetNo; 
		}
	}
	//检查SD卡是否插入
	public static boolean checkSDCardInsert(){
		if(!Environment.getExternalStorageState().equals("mounted")){
			return false;
		}
		return true;
	}
	//SD剩余空间
	public static double getSDFreeSpace() {
		double sdSize;
		if(Environment.getExternalStorageState().equals("mounted")) {
			StatFs statfs = new StatFs(Environment.getExternalStorageDirectory().getPath());
			long blocksize = statfs.getBlockSize();
			sdSize = ((double)statfs.getAvailableBlocks() * (double)blocksize) / 1024D / 1024D;
		} 
		else{
			sdSize = 0D;
		}
		return sdSize;
	}
	//得到手机设别标识符
	public static String getDeviceID(Context context){
		TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
		return telephonyManager.getDeviceId() 
//				+ "\n" +  telephonyManager.getLine1Number() + 
//				"\n" +  telephonyManager.getSimSerialNumber() + 
//				"\n"+ telephonyManager.getSubscriberId()
				;
	}
}
